package ${domain}.module.wsys.mgr;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ${domain}.frame.base.ErrorType;
import ${domain}.frame.auth.Token;
import ${domain}.frame.utils.IDgenerator;
import ${domain}.frame.utils.MapperUtil;
import ${domain}.frame.utils.Message;
import ${domain}.frame.utils.ValidationUtil;
import ${domain}.module.wsys.ent.Role;
import ${domain}.module.wsys.mpr.RoleMapper;
import ${domain}.module.wsys.req.RoleCreateRequest;
import ${domain}.module.wsys.req.RoleDeleteRequest;
import ${domain}.module.wsys.req.RoleFindRequest;
import ${domain}.module.wsys.req.RoleLoadRequest;
import ${domain}.module.wsys.req.RoleUpdateRequest;
import ${domain}.module.wsys.rsp.RoleCreateResponse;
import ${domain}.module.wsys.rsp.RoleDeleteResponse;
import ${domain}.module.wsys.rsp.RoleFindResponse;
import ${domain}.module.wsys.rsp.RoleLoadResponse;
import ${domain}.module.wsys.rsp.RoleUpdateResponse;

import java.util.List;

/**
 * ROLE - 角色
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
@Transactional
@Service
public class RoleManagerImpl implements RoleManager {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 插入
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public RoleCreateResponse create(RoleCreateRequest request, Token token) {
        RoleCreateResponse response = new RoleCreateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        {//检查Code是否重复
            RoleFindRequest roleFindRequest = new RoleFindRequest();
            roleFindRequest.setCode(request.getCode());
            RoleFindResponse roleFindResponse = find(roleFindRequest, token);
            if (roleFindResponse.hasError()) {
                response.addErrors(roleFindResponse.getErrors());
                return response;
            } else if (roleFindResponse.getResult().size() > 0) {
                response.addError(ErrorType.BUSINESS_ERROR, "[" + request.getCode() + "]已存在");
                return response;
            }
        }

        long id = IDgenerator.nextId();
        Role entity = MapperUtil.map(request, Role.class);
        entity.setId(id);

        long result = roleMapper.insert(entity, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.CREATE_FAILURE);
            return response;
        }
        response.setId(id);

        return response;
    }

    /**
     * 逻辑删除
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public RoleDeleteResponse delete(RoleDeleteRequest request, Token token) {
        RoleDeleteResponse response = new RoleDeleteResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        long result = roleMapper.delete(request.getId(), token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.DELETE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }

    /**
     * 更新
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public RoleUpdateResponse update(RoleUpdateRequest request, Token token) {
        RoleUpdateResponse response = new RoleUpdateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        Role entity = roleMapper.getById(request.getId(), token);
        if (entity == null) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.GET_FAILURE);
            return response;
        }

        MapperUtil.map(request, entity);
        long result = roleMapper.update(entity, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.UPDATE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }

    /**
     * 查询
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    @Transactional(readOnly = true)
    public RoleFindResponse find(RoleFindRequest request, Token token) {
        RoleFindResponse response = new RoleFindResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        if (request.getPageSize() != 0) {
            PageHelper.startPage(request.getPageNumber(), request.getPageSize());
        }
        if (StringUtil.isNotEmpty(request.getSortKey())) {
            PageHelper.orderBy(request.getSortKey() + " " + request.getSortType());
        }
        PageInfo<Role> pageInfo = new PageInfo<>(roleMapper.find(request, token));

        response.setResult(pageInfo.getList());
        response.setTotalCount(pageInfo.getTotal());

        return response;
    }

    /**
     * 加载
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    @Transactional(readOnly = true)
    public RoleLoadResponse load(RoleLoadRequest request, Token token) {
        RoleLoadResponse response = new RoleLoadResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        List<Role> result = roleMapper.select(new Role(), token);

        response.setResult(result);
        response.setTotalCount((long) result.size());

        return response;
    }
}
